<?php
/**
 * Filesystem functions
 *
 * @version 0.2a
 */

// No direct access
defined( "__DEENZO__" ) or die( "deenzo$" ); 

/* Based on: http://www.php.net/manual/en/function.exec.php#43834 */
function bash( $sh, $args = "" ) {

    /* Parse ARGS */
    eval( ARGL );
    
    /* Windows manage SHELL execution different */
    if ( preg_match( "/^WIN/", PHP_OS ) )
        $bash = ( $m ) ? pclose( popen( "start 'bla' '$sh'", "r" ) )
                       : pclose( popen( "start 'bla' '$sh'", "r" ) );
    else 
        $bash = ( $m ) ? exec( "$sh > /dev/null & uname" ) 
                       : exec( $sh );
    
    // Only if not Multitasking                 
    if ( !$m ) return $bash;

} 

/** Removes a file or directory */
function rm( $e, $args = "" ) {
    
    /* Parse ARGS */
    eval( ARGL );
    
    /* If file, remove it */
    if ( is_file( $e ) )
        return unlink( $e );
    
    elseif ( is_dir( $e ) ) {
    
        if ( !$R )
            return rmdir( $e );
            
        else {
        
            foreach( (array) scandir( $e ) as $d ) {
            
                if ( ( $d != "." ) && ( $d != ".." ) ) {
                
                    if ( is_dir( "$e/$d" ) )
                        rm( "$e/$d", "-R" );
                    else
                        rm( "$e/$d" );
                }
                
            } rm( "$e" );
        }
    }
}

/* Remove MacOS system files */
function unmac( $dir ) {

    /* Remove MacOS in non Darwin */
    if ( PHP_OS == "Darwin" )
        return false;

	foreach ( scandir( $dir ) as $subd ) {
	
		if ( ( $subd != "." ) and ( $subd != ".." ) ) {
			
			if ( is_dir( "$dir/$subd" ) )
				unmac( "$dir/$subd" );
			
			/* Removes .DS_STORE */
			elseif ( ( $subd == ".DS_Store" )
				or   ( $subd == "._.DS_Store" )
				or   ( preg_match( "/^\._/", $subd ) ) )
			    unlink("$dir/$subd");
		
		}
	}
}

/* Remove SVN directories */
function unsvn( $dir ) {
    
    foreach ( scandir( $dir ) as $subd ) {
    
        if ( ( $subd != "." ) and ( $subd != ".." ) ) {
			
			if ( is_dir( "$dir/$subd" ) )
				unsvn( "$dir/$subd" );
			
            // Ignore SVN directory
			if ( strpos( "$dir/$subd", "/svn/" ) < 1 ) {
				if ( $subd == ".svn" )
					rm( "$dir/$subd", "-R" );
			}
		}
    }
}